<?php

/**
 * URL解析处理类
 * 根据URI，提取出主体程序参数
 * @author bing.peng
 *
 */
class URI {
	
	private $config;
	private $allowed = FALSE;
 	private $source;
 	private $class;
 	private $method = 'index';
 	/** 缓存时间 **/
 	private $cache = 7200;
 	
 	/** 钩子 */
 	private $hooks = array();
 	
	private $uri;
	private $url_this;
	private $params = array();
	
	
	function URI() {
		$this->config = get_config();
		
		$this->parse();
	}
	
	/**
	 * 获取程序action源文件地址
	 * @return string
	 */
	function fetch_source() {
		return $this->source;
	}
	
	/**
	 * 获取action类名
	 * @return string
	 */
	function fetch_class() {
		return $this->class;
	}
	
	/**
	 * 获取action方法
	 * @return string
	 */
	function fetch_method() {
		return $this->method;
	}
	
	/**
	 * 获取钩子
	 * @return array()
	 */
	function fetch_hooks() {
		return $this->hooks;
	}
	
	function fetch_params() {
		return empty($this->params) ? false : $this->params;
	}

	function fetch_cache() {
		return $this->cache;
	}
	
	/**
	 * 解析URL提取参数
	 * @return void
	 */
	protected function parse() {
		// 获取协议头
		$http_protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ) ? 'https://' : 'http://';
		$this->url_this = $http_protocol . $_SERVER ['HTTP_HOST'] . $_SERVER['PHP_SELF'];		
		
		$this->uri = str_replace( $this->config['base_url'] . $this->config['index_page'] . '/', '', $this->url_this );
		
		
		/** 导入URL映射 */
		require( ABSPATH . 'urls.php');
		
		foreach( $urls as $key => $value ) {
			$url_pattern = preg_replace('/<\w+>/e', '${1}', $key);
			$url_pattern = '/' . $url_pattern . '/';
			
			if( preg_match($url_pattern, $this->uri) ) {
				$this->get_url_params($key);
				
				$this->source = $value['src'];
				$this->class = $value['class'];
				
				if( isset( $value['func'] ) ) {
					$this->method = $value['func'];	
				}
				
				// 获取参数
				if( isset($value['params']) ) {
					$params_str = $value['params'];
					$params_array = explode(',', $params_str);
					foreach($params_array as $param) {
						$temp_array = explode('=', $param);
						$this->params[ $temp_array[0] ] = $temp_array[1];
					}
				}
				
				// 获取钩子列表
				if( isset( $value['hook'] ) ) {
					$this->hooks = explode(',', $value['hook']);
				}
				
				// 获取缓存参数
				if( isset( $value['cache'] ) ) {
					$this->cache = $value['cache'];
				}
				
				$this->allowed = true;
				break;
			}
		}
		
		// 没有相匹配的URI，响应404页面
		if( ! $this->allowed ) {
			show_error_404();
		}
	}
	
	/**
	 * 获取URL中定义的参数
	 * @param $pattern
	 * @return void
	 */
	function get_url_params($pattern) {	
		$pattern_array = preg_split('/\//', $pattern);
		$uri_array = preg_split('/\//', $this->uri);
		
		$i = 0;
		foreach($pattern_array as $pattern_segment) {
			if( preg_match('/<\w+>*+/', $pattern_segment, $param_array) ) {
				$param_key = substr($param_array[0], 1, strlen($param_array[0])-2);
				$this->params[$param_key] = $uri_array[$i];
				//echo "param_key:" . $param_key . "  value:" .$uri_array[$i];
			}
			$i++;
		}
	}
	
	
}



?>